import WebClient from '@/api/WebClient';
import store from '@/store';
const baseURL = '/ctp/selectorg';

/**
 * 选人组件接口发送请求时处理includeElements，excludeElements
 * 返回 includeElements(Array)，excludeElements(Array)
 */
const checkInEnElements = () => {
	let elements = {};
	let includeElements = store.state.selectData.selectParams.includeElements;
	let excludeElements = store.state.selectData.selectParams.excludeElements;
	if (includeElements && includeElements.length) {
		elements.includeElements = includeElements;
	}
	if (excludeElements && excludeElements.length) {
		elements.excludeElements = excludeElements;
	}
	return elements;
};

export const getPanelInfo = (params) => {
	return WebClient.post(`${baseURL}/panel`, params);
};
export const getPanelData = (params) => {
	/**
   * 选人组件接口统一添加参数
   */
	let requestParams = JSON.parse(JSON.stringify(params));
	let elements = checkInEnElements();
	if (Object.prototype.hasOwnProperty.call(requestParams, 'arguments')) {
		requestParams.arguments = { ...requestParams.arguments, ...elements };
	}
	return new Promise(async (resolve, reject) => {
		let start = Date.now();
		try {
			let data = await WebClient.post(`${baseURL}/panel/datas`, requestParams);
			resolve(data);
		} catch (e) {
			console.error(`GetPanelData:${requestParams.panel}_${requestParams.command} error!`,e);
			resolve({
				message: `GetPanelData '${requestParams.panel}_${requestParams.command}' error!`,
				code: -1,
				data: []
			});
		}
	});
};
export const parseEntity = (params) => {
	/**
   * 选人组件接口统一添加参数
   */
	let requestParams = JSON.parse(JSON.stringify(params));
	let elements = checkInEnElements();
	if (Object.prototype.hasOwnProperty.call(requestParams, 'arguments')) {
		requestParams.arguments = { ...requestParams.arguments, ...elements };
	}
	return WebClient.post(`${baseURL}/panel/parse`, requestParams);
};
export const doCheckRelation = (params) => {
	return WebClient.post(`${baseURL}/panel/relation/check`, params);
};
export const doCheckScope = (params) => {
	return WebClient.post(`${baseURL}/panel/select/check`, params);
};


